package device;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import db.DataBaseServlet;
import user.WebsocketServe;

public class DeviceFactory {
	
	/**
	 * 设置电源状态
	 * @param deviceId
	 * @param power
	 */
	public static void setPower(int deviceId, boolean power) {
		final String SQL_UPDATE = "update device set power = %b where id = %d;";
		
		DataBaseServlet db = DataBaseServlet.DB;
		// 创建查询对象
		Statement stmt = db.newQuery();
		if(stmt == null) {
			return;
		}
		
		//	基础信息
		Boolean suc = true;
		try {
			String sql = String.format(SQL_UPDATE, power, deviceId);
			if (stmt.executeUpdate(sql) == 0) {
				throw new SQLException();
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
			suc = false;
		}
		if(suc) {
			// 通知用户 设备电源变化
			final String DATA = "{\"target\": \"device\",\"device\": [{\"deviceId\": %d,\"power\":%b}]}";
			WebsocketServe.broadcast(String.format(DATA, deviceId, power));
			
		}
		
		System.out.print(String.format("\n[setPower]deviceId=%d, power=%b", deviceId, power));
	}
	
	/**
	 * 设置在线状态
	 * @param deviceId
	 * @param online
	 */
	public static void setOnline(int deviceId, boolean online) {
		final String SQL_UPDATE = "update device set online = %b where id = %d;";
		
		DataBaseServlet db = DataBaseServlet.DB;
		// 创建查询对象
		Statement stmt = db.newQuery();
		if(stmt == null) {
			return;
		}
		//	基础信息
		Boolean suc = true;
		try {
			String sql = String.format(SQL_UPDATE, online, deviceId);
			if (stmt.executeUpdate(sql) == 0) {
				throw new SQLException();
			}
			stmt.close();
		}
		catch (SQLException e) {
			e.printStackTrace();
			suc = false;
		}
		if(suc) {
			// 通知用户 设备网络连接变化
			final String DATA = "{\"target\": \"device\",\"device\": [{\"deviceId\": %d,\"online\":%b}]}";
			WebsocketServe.broadcast(String.format(DATA, deviceId, online));
			
		}
		System.out.print(String.format("\n[setOnline]deviceId=%d, online=%b", deviceId, online));
	}
	
	/**
	 * 设置设备属性
	 * @param deviceId
	 * @param attrId
	 * @param value
	 */
	public static void setAttribute(int deviceId, HashMap<Integer, String> sites) {
		final String SQL_UPDATE = "UPDATE device_attribute SET value = '%s' WHERE (attributeId = %d and deviceId = %d);";
		
		DataBaseServlet db = DataBaseServlet.DB;
		// 创建查询对象
		Statement stmt = db.newQuery();
		if(stmt == null) {
			return;
		}
		Boolean suc = true;
		try {
			//StringBuffer buffer = new StringBuffer();
			String sql = null;
			for (Integer i : sites.keySet()) {
	            //buffer.append(String.format(SQL_UPDATE, sites.get(i), i, deviceId));
				sql = String.format(SQL_UPDATE, sites.get(i), i, deviceId);
				System.out.println(sql);
				if (stmt.executeUpdate(sql) == 0) {
					throw new SQLException();
				}
			}
			//String sql = buffer.toString();
			
			stmt.close();
		}
		catch (SQLException e) {
			e.printStackTrace();
			suc = false;
		}
		if(suc) {
			// 通知用户 设备属性变化
			final String DATA = "{\"target\":\"session\",\"deviceId\":%d,\"detail\":[%s]}";
			final String DETAIL = "{\"id\":%d,\"value\":\"%s\"},";
			StringBuffer buffer = new StringBuffer();
			//buffer.append(String.format(HEAD, ))
			for (Integer i : sites.keySet()) {
	            buffer.append(String.format(DETAIL, i, sites.get(i)));
	        }
			buffer.deleteCharAt(buffer.length() -1);
			String data = String.format(DATA, deviceId, buffer.toString());
			System.out.println(data);
			WebsocketServe.broadcast(data);
			
		}
	}
	
	/**
	 * 客户端切换电源状态
	 * @param deviceId
	 * @param power
	 */
	public static void switchPower(int deviceId, Boolean power) {
		char[] data = {(char) deviceId, 1, (char) (power? 1:2)};
		String msg = new String(data);
		device.DeviceEndpointServlet.broadcast(msg);
	}
	
}
